package com.yzcrm.erp.psi.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Aop;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.common.constant.BaseConstant;
import com.yzcrm.erp.admin.service.AdminRoleService;
import com.yzcrm.erp.admin.service.AdminSceneService;
import com.yzcrm.erp.crm.common.CrmEnum;
import com.yzcrm.utils.AuthUtil;
import com.yzcrm.utils.BaseUtil;
import com.yzcrm.utils.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiang.xue
 */
public class PsiBackLogService {

    @Inject
    PsiEnterLeaveStockService psiEnterLeaveStockService;
    @Inject
    AdminSceneService adminSceneService;

    /**
     * 待办事项数量统计
     */
    public R num() {

        Long userId = BaseUtil.getUserId();
        JSONObject jsonObject= Aop.get(AdminRoleService.class).auth(userId);
        List<String> authList= AuthUtil.queryAuth(jsonObject, "");
        Kv kv = new Kv();

        // 待审核出库
        // 待审核入库

        // 20销售退货 salesEnter
        if (authList.contains("psi:salesEnter:index")){

            Integer checkSalesEnter = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_SALES_ENTER.getType());
            kv.set("checkSalesEnter",checkSalesEnter);
        }

        // 21归还入库 retrunEnter
        if (authList.contains("psi:retrunEnter:index")){

            Integer checkRetrunEnter = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_RETRUN_ENTER.getType());
            kv.set("checkRetrunEnter",checkRetrunEnter);
        }

        // 22维修入库repairEnter
        if (authList.contains("psi:repairEnter:index")){

            Integer checkRepairEnter = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_REPAIR_ENTER.getType());
            kv.set("checkRepairEnter",checkRepairEnter);
        }

        // 23内部测试入库testEnter
        if (authList.contains("psi:testEnter:index")){

            Integer checkTestEnter = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_TEST_ENTER.getType());
            kv.set("checkTestEnter",checkTestEnter);
        }

        // 24盘盈inventoryProfitEnter
        if (authList.contains("psi:inventoryProfitEnter:index")){

            Integer checkInventoryProfitEnter = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_INVENTORY_PROFIT_ENTER.getType());
            kv.set("checkInventoryProfitEnter",checkInventoryProfitEnter);
        }

        // 25其它入库otherEnter
        if (authList.contains("psi:otherEnter:index")){

            Integer checkOtherEnter = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_OTHER_ENTER.getType());
            kv.set("checkOtherEnter",checkOtherEnter);
        }

        // 30采购入库purchaseEnter
        if (authList.contains("psi:purchaseEnter:index")){

            Integer checkPurchaseEnter = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_PURCHASE_ENTER.getType());
            kv.set("checkPurchaseEnter",checkPurchaseEnter);
        }

        // 40销售出库salesLeave
        if (authList.contains("psi:salesLeave:index")){

            Integer checkSalesLeave = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_SALES_LEAVE.getType());
            kv.set("checkSalesLeave",checkSalesLeave);
        }

        // 41借用出库borrowLeave
        if (authList.contains("psi:borrowLeave:index")){

            Integer checkBorrowLeave = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_BORROW_LEAVE.getType());
            kv.set("checkBorrowLeave",checkBorrowLeave);
        }

        // 42维修出库repairLeave
        if (authList.contains("psi:repairLeave:index")){

            Integer checkRepairLeave = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_REPAIR_LEAVE.getType());
            kv.set("checkRepairLeave",checkRepairLeave);
        }

        // 43内部测试出库testLeave
        if (authList.contains("psi:testLeave:index")){

            Integer checkTestLeave = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_TEST_LEAVE.getType());
            kv.set("checkTestLeave",checkTestLeave);
        }

        // 44盘亏出库 inventoryLossLeave
        if (authList.contains("psi:inventoryLossLeave:index")){

            Integer checkInventoryLossLeave = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_INVENTORY_LOSS_LEAVE.getType());
            kv.set("checkInventoryLossLeave",checkInventoryLossLeave);
        }

        // 45其它出库otherLeave
        if (authList.contains("psi:otherLeave:index")){

            Integer checkOtherLeave = Db.queryInt(Db.getSql("psi.backLog.checkEnterLeaveStockNum"),userId, CrmEnum.PSI_OTHER_LEAVE.getType());
            kv.set("checkOtherLeave",checkOtherLeave);
        }

        return R.ok().put("data", kv);
    }

    /**
     * 待审核进销存
     * @author lg
     */
    public R checkEnterLeaveStock(BasePageRequest basePageRequest) {

    	JSONObject jsonObject = basePageRequest.getJsonObject();
    	Integer checkType = jsonObject.getInteger("checkType");
        Integer isSub = jsonObject.getInteger("isSub");
        StringBuffer stringBuffer = new StringBuffer("select els_id from yzcrm_psi_enter_leave_stock as a inner join yzcrm_admin_examine_record as b on a.examine_record_id = b.record_id left join yzcrm_admin_examine_log as c on b.record_id = c.record_id where c.is_recheck != 1 and ifnull(b.examine_step_id, 1) = ifnull(c.examine_step_id, 1) and");
        if (checkType == 1){
            stringBuffer.append(" a.check_status in (0,3) and c.examine_status in (0,3)");
        }else if (checkType == 2){
            stringBuffer.append(" a.check_status in (1,2)");
        }else {
            return R.error("checkType类型不正确");
        }
        if (isSub == 1){
            stringBuffer.append(" and c.examine_user = ").append(BaseUtil.getUserId());
        }else if (isSub == 2){
            String ids = adminSceneService.getSubUserId(BaseUtil.getUserId(), BaseConstant.AUTH_DATA_RECURSION_NUM).substring(1);
            stringBuffer.append(" and c.examine_user in (").append(ids).append(")");
        }else {
            return R.error("isSub参数不正确");
        }
        Integer page = basePageRequest.getPage();
        Integer limit = basePageRequest.getLimit();
        stringBuffer.append(" limit ").append((page - 1) * limit).append(",").append(limit);
        List<Integer> elsIds = Db.query(stringBuffer.toString());
        if (elsIds.size() == 0){
            return R.ok().put("data",new ArrayList<>());
        }
        JSONObject data = jsonObject.getJSONObject("data")!=null ? jsonObject.getJSONObject("data") : new JSONObject();

        data.fluentPut("backLog",new JSONObject().fluentPut("name","els_id").fluentPut("condition","in").fluentPut("value",StrUtil.join(",",elsIds)));
        jsonObject.fluentPut("data",data).fluentPut("search","");
        basePageRequest.setJsonObject(jsonObject);
        return psiEnterLeaveStockService.getEnterLeaveStockPageList(basePageRequest);
    }



}
